package com.hangxiang.approval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangxiang.approval.common.IntegerCommon;
import com.hangxiang.approval.common.SecondMenuCommon;
import com.hangxiang.approval.common.SecondMenuInsertCommon;
import com.hangxiang.approval.common.SecondMenuUpdateCommon;
import com.hangxiang.approval.dao.SecondMenuDao;
import com.hangxiang.approval.entity.Approve;
import com.hangxiang.approval.entity.FirstMenu;
import com.hangxiang.approval.entity.SecondMenu;
import com.hangxiang.approval.service.SecondMenuService;
import com.hangxiang.approval.utils.BeanConvertUtils;
import com.hangxiang.approval.vo.FirstMenuVo;
import com.hangxiang.approval.vo.SecondMenuVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * (SecondMenu)表服务实现类
 *
 * @author makejava
 * @since 2020-11-25 20:04:11
 */
@Service("secondMenuService")
public class SecondMenuServiceImpl extends ServiceImpl<SecondMenuDao, SecondMenu> implements SecondMenuService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SecondMenuDao secondMenuDao;

    /**
     * 通过ID查询单条数据
     *
     * @param common 主键
     * @return 实例对象
     */
    @Override
    public SecondMenuVo queryById(IntegerCommon common) {
        SecondMenuVo vo = new SecondMenuVo();
        SecondMenu secondMenu = secondMenuDao.selectById(common.getValue());
        BeanUtils.copyProperties(secondMenu, vo);
        return vo;
    }


    /**
     * 新增数据
     *
     * @param secondMenuInsertCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer insertSecondMenu(SecondMenuInsertCommon secondMenuInsertCommon) {
        SecondMenu secondMenu = new SecondMenu();
        BeanUtils.copyProperties(secondMenuInsertCommon, secondMenu);
        secondMenuDao.insert(secondMenu);
        return secondMenu.getId();
    }

    /**
     * 修改数据
     *
     * @param secondMenuUpdateCommon 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Integer updateSecondMenu(SecondMenuUpdateCommon secondMenuUpdateCommon) {
        SecondMenu secondMenu = new SecondMenu();
        BeanUtils.copyProperties(secondMenuUpdateCommon, secondMenu);
        secondMenuDao.updateById(secondMenu);
        return secondMenu.getId();
    }
    /**
     * 查询二级菜单
     *
     * @param
     * @return 实例对象
     */
    @Override
    public List<SecondMenuVo> selectAllSecondMenu(SecondMenuCommon common) {
        List<SecondMenu> secondMenus=secondMenuDao.selectList(new QueryWrapper<SecondMenu>().lambda().eq(SecondMenu::getFirstId,common.getFirstId()));
        List<SecondMenuVo> firstMenuVoList = BeanConvertUtils.convertListTo(secondMenus, SecondMenuVo::new);
        return firstMenuVoList;
    }
}